package designPattern.singleton;

public class SingletonTest4 {
    public static void main(String[] args) {
        Singleton4 instance1 = Singleton4.getInstance();
        Singleton4 instance2 = Singleton4.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（线程安全，同步方法）

class Singleton4{
    private static Singleton4 instance;

    private Singleton4(){}

    //提供一个静态的公共方法，加入同步处理的方法，解决线程同步问题
    public static synchronized Singleton4 getInstance(){
        if(instance==null){
            instance = new Singleton4();
        }

        return instance;
    }
}